<script setup lang="ts">
import { getCartListApi, deleteCartApi, updateCartApi } from '@/services/cartAPI'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import { useMemberStore } from '@/stores'
import type { cartListType } from '@/types/cart'
import { computed } from 'vue'
import { useCartStore } from '@/stores/modules/CartStore'
import { debounce } from '@/utils/handleIncrement'
// 会员信息
const userMemberStore = useMemberStore()
const cartList = ref<cartListType[]>([])
const getCartListData = async () => {
  const res = await getCartListApi()
  console.log(res.data)
  cartList.value = res.data as cartListType[]
}
// 这里还是要后端存储selected字段比较好，所以这里就不做映射了，后端给了selected字段
const handleSelect = (cart: cartListType) => {
  cart.selected = !cart.selected
}

// 全选状态
const isAll = ref(false)

// 全选
const handleSelectAll = () => {
  if (isAll.value) {
    cartList.value.forEach((cart) => {
      cart.selected = false
    })
    isAll.value = false
    return
  }
  cartList.value.forEach((cart) => {
    cart.selected = true
  })
  isAll.value = true
}
onLoad(async () => {
  await getCartListData()
})
// 总价格
const total_price = computed(() => {
  let total = 0
  cartList.value.forEach((cart) => {
    if (cart.selected) {
      total += cart.sku_data.price * cart.num
    }
  })
  return total
})
// 动态计算购买数量
const total_num = computed(() => {
  let total_num = 0
  cartList.value.forEach((cart) => {
    if (cart.selected) {
      total_num += 1
    }
  })
  return total_num
})

const handleDelete = async (cart_id: number) => {
  const res = await deleteCartApi(cart_id)
  if (res.code === 200) {
    uni.showToast({
      title: '删除成功',
      icon: 'success',
      duration: 2000,
    })
    getCartListData()
  } else {
    uni.showToast({
      title: res.message,
      icon: 'error',
      duration: 2000,
    })
  }
}
// 防抖函数，300毫秒，防止用户频繁点击增加减少按钮
const handleDecrement = debounce(async (cart: cartListType, type: string) => {
  if (type === 'subtraction') {
    if (cart.num > 1) {
      cart.num--
    }
  } else if (type === 'addition') {
    if (cart.num < cart.sku_data.stock) {
      cart.num++
    }
  }
  await updateCartApi(cart.id, cart.num)
})

const gotoOrder = () => {
  // 进入订单页前把需要的商品信息传入
  const paramsSku = ref<number[]>([])
  cartList.value.forEach((cart) => {
    if (cart.selected && cart.num > 0) {
      console.log(cart.sku_data._id)
      // 后端的设计需要skuid，通过sku也可以获得属性
      paramsSku.value?.push(cart.sku_data._id)
    }
  })
  uni.navigateTo({
    // 跳转路由，需要把sku_id_list传入，但是地址栏只能以字符串的形式传递，所以需要转为json字符串
    // 所需页面接收后需要把json字符串解析
    url: `/pages/order/order?sku_id_list=${JSON.stringify(paramsSku.value)}`,
  })
}
</script>

<template>
  <scroll-view scroll-y class="scroll-view">
    <!-- 已登录: 显示购物车 -->
    <template v-if="userMemberStore.getToekn()">
      <!-- 购物车列表 -->
      <view class="cart-list" v-if="cartList.length > 0">
        <!-- 优惠提示 -->
        <view class="tips">
          <text class="label">满减</text>
          <text class="desc">满1件, 即可享受9折优惠</text>
        </view>
        <!-- 滑动操作分区 -->
        <uni-swipe-action>
          <!-- 滑动操作项 -->
          <uni-swipe-action-item v-for="cart in cartList" :key="cart.id" class="cart-swipe">
            <!-- 商品信息 -->
            <view class="goods">
              <!-- 选中状态 -->
              <text
                class="checkbox"
                :class="{ checked: cart.selected }"
                @tap="handleSelect(cart)"
              ></text>
              <navigator
                :url="`/pages/goods/goods?goods_id=${cart.goods_info.id}`"
                hover-class="none"
                class="navigator"
              >
                <image mode="aspectFill" class="picture" :src="cart.sku_data.image"></image>
                <view class="meta">
                  <view class="name ellipsis">{{ cart.goods_info.name }}</view>
                  <view class="attrsText ellipsis">{{ cart.sku_data.sku_name_arr.join('-') }}</view>
                  <view class="price">{{ cart.sku_data.price }}</view>
                </view>
              </navigator>
              <!-- 商品数量 -->
              <view class="count">
                <text class="text" @tap="handleDecrement(cart, 'subtraction')">-</text>
                <input class="input" type="number" v-model="cart.num" />
                <text class="text" @tap="handleDecrement(cart, 'addition')">+</text>
              </view>
            </view>
            <!-- 右侧删除按钮 -->
            <template #right>
              <view class="cart-swipe-right">
                <button class="button delete-button" @tap="handleDelete(cart.id)">删除</button>
              </view>
            </template>
          </uni-swipe-action-item>
        </uni-swipe-action>
      </view>
      <!-- 购物车空状态 -->
      <view class="cart-blank" v-else>
        <image src="/static/images/blank_cart.png" class="image" />
        <text class="text">购物车还是空的，快来挑选好货吧</text>
        <navigator url="/pages/index/index" hover-class="none">
          <button class="button">去首页看看</button>
        </navigator>
      </view>
      <!-- 吸底工具栏 -->
      <view class="toolbar">
        <text class="all" :class="{ checked: isAll }" @tap="handleSelectAll">全选</text>
        <text class="text">合计:</text>
        <text class="amount">{{ total_price }}</text>
        <view class="button-grounp">
          <view
            class="button payment-button"
            :class="total_num > 0 ? '' : 'disabled'"
            @tap="gotoOrder"
          >
            去结算({{ total_num }})
          </view>
        </view>
      </view>
    </template>
    <!-- 未登录: 提示登录 -->
    <view class="login-blank" v-else>
      <text class="text">登录后可查看购物车中的商品</text>
      <navigator url="/pages/login/login" hover-class="none">
        <button class="button">去登录</button>
      </navigator>
    </view>
    <!-- 猜你喜欢 -->
    <XtxGuess ref="guessRef"></XtxGuess>
    <!-- 底部占位空盒子 -->
    <view class="toolbar-height"></view>
  </scroll-view>
</template>

<style lang="scss">
// 根元素
:host {
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background-color: #f7f7f8;
}

// 滚动容器
.scroll-view {
  flex: 1;
}

// 购物车列表
.cart-list {
  padding: 0 20rpx;

  // 优惠提示
  .tips {
    display: flex;
    align-items: center;
    line-height: 1;
    margin: 30rpx 10rpx;
    font-size: 26rpx;
    color: #666;

    .label {
      color: #fff;
      padding: 7rpx 15rpx 5rpx;
      border-radius: 4rpx;
      font-size: 24rpx;
      background-color: #27ba9b;
      margin-right: 10rpx;
    }
  }

  // 购物车商品
  .goods {
    display: flex;
    padding: 20rpx 20rpx 20rpx 80rpx;
    border-radius: 10rpx;
    background-color: #fff;
    position: relative;

    .navigator {
      display: flex;
    }

    .checkbox {
      position: absolute;
      top: 0;
      left: 0;

      display: flex;
      align-items: center;
      justify-content: center;
      width: 80rpx;
      height: 100%;

      &::before {
        content: '\e6cd';
        font-family: 'erabbit' !important;
        font-size: 40rpx;
        color: #444;
      }

      &.checked::before {
        content: '\e6cc';
        color: #27ba9b;
      }
    }

    .picture {
      width: 170rpx;
      height: 170rpx;
    }

    .meta {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      margin-left: 20rpx;
    }

    .name {
      height: 72rpx;
      font-size: 26rpx;
      color: #444;
    }

    .attrsText {
      line-height: 1.8;
      padding: 0 15rpx;
      font-size: 24rpx;
      align-self: flex-start;
      border-radius: 4rpx;
      color: #888;
      background-color: #f7f7f8;
    }

    .price {
      line-height: 1;
      font-size: 26rpx;
      color: #444;
      margin-bottom: 2rpx;
      color: #cf4444;

      &::before {
        content: '￥';
        font-size: 80%;
      }
    }

    // 商品数量
    .count {
      position: absolute;
      bottom: 20rpx;
      right: 5rpx;

      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 220rpx;
      height: 48rpx;

      .text {
        height: 100%;
        padding: 0 20rpx;
        font-size: 32rpx;
        color: #444;
      }

      .input {
        height: 100%;
        text-align: center;
        border-radius: 4rpx;
        font-size: 24rpx;
        color: #444;
        background-color: #f6f6f6;
      }
    }
  }

  .cart-swipe {
    display: block;
    margin: 20rpx 0;
  }

  .cart-swipe-right {
    display: flex;
    height: 100%;

    .button {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 50px;
      padding: 6px;
      line-height: 1.5;
      color: #fff;
      font-size: 26rpx;
      border-radius: 0;
    }

    .delete-button {
      background-color: #cf4444;
    }
  }
}

// 空状态
.cart-blank,
.login-blank {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  height: 60vh;
  .image {
    width: 400rpx;
    height: 281rpx;
  }
  .text {
    color: #444;
    font-size: 26rpx;
    margin: 20rpx 0;
  }
  .button {
    width: 240rpx !important;
    height: 60rpx;
    line-height: 60rpx;
    margin-top: 20rpx;
    font-size: 26rpx;
    border-radius: 60rpx;
    color: #fff;
    background-color: #27ba9b;
  }
}

// 吸底工具栏
.toolbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--window-bottom));
  z-index: 1;

  height: 100rpx;
  padding: 0 20rpx;
  display: flex;
  align-items: center;
  border-top: 1rpx solid #ededed;
  border-bottom: 1rpx solid #ededed;
  background-color: #fff;

  .all {
    margin-left: 25rpx;
    font-size: 14px;
    color: #444;
    display: flex;
    align-items: center;
  }

  .all::before {
    font-family: 'erabbit' !important;
    content: '\e6cd';
    font-size: 40rpx;
    margin-right: 8rpx;
  }

  .checked::before {
    content: '\e6cc';
    color: #27ba9b;
  }

  .text {
    margin-right: 8rpx;
    margin-left: 32rpx;
    color: #444;
    font-size: 14px;
  }

  .amount {
    font-size: 20px;
    color: #cf4444;

    .decimal {
      font-size: 12px;
    }

    &::before {
      content: '￥';
      font-size: 12px;
    }
  }

  .button-grounp {
    position: absolute;
    right: 10rpx;
    top: 50%;

    display: flex;
    justify-content: space-between;
    text-align: center;
    line-height: 72rpx;
    font-size: 13px;
    color: #fff;
    transform: translateY(-50%);

    .button {
      width: 240rpx;
      margin: 0 10rpx;
      border-radius: 72rpx;
    }

    .payment-button {
      background-color: #27ba9b;

      &.disabled {
        opacity: 0.6;
      }
    }
  }
}
// 底部占位空盒子
.toolbar-height {
  height: 100rpx;
  box-sizing: content-box;
}
</style>
